import { search } from '../../apis/api/index';

const app = getApp()
Page({
  data: {
    page: 0,
    tableData: [],
    totalPageNum: '',
    actived: 0,
    searchStr: '',
    storeId: ''
  },
  handleSearchDisplay() {
    wx.navigateBack()
  },
  handleAdd(e) {
    const { item, type } = e.currentTarget.dataset
    const itinerary = wx.getStorageSync('itinerary') || []
    Object.assign(item, {
      address: item[type + 'Address'],
      vialons: item[type + 'Longitude'],
      vialats: item[type + 'Latitude']
    })
    if (!itinerary.length || !itinerary.find(v => (item.customerNumber + item.address) === (v.customerNumber + v.address))) {
      itinerary.push(item)
    }
    wx.setStorageSync('itinerary', itinerary)
    app.showToast('添加成功')
  },
  async init() {
    let { tableData, page, searchStr } = this.data
    const params = {
      page: page + 1,
      limit: 10,
      customerNumber: searchStr,
    }
    const data = await search(params)

    tableData = tableData.concat([...data.list])
    this.setData({
      tableData: tableData,
      page: params.page,
      totalSize: data.totalSize,
      moreBtnDispaly: data.totalSize !== tableData.length
    })
  },
  onLoad: function (options) {
    if (options) {
      this.setData({
        searchStr: options.search
      })
      this.init()
    }
  }
})